package com.kehutong.common;

import java.time.LocalDateTime;
import java.util.Objects;

import org.coraframework.util.HashID;

/**
 * Author:  peter
 * Date:    2020/7/20
 * Function:
 */
public class RemarkBuilder {

    private StringBuilder sb = new StringBuilder();


    public RemarkBuilder append(String name, LocalDateTime from, LocalDateTime to) {
        return append(name, Java8TimeUtils.parseString(from), Java8TimeUtils.parseString(to));
    }

    public <T> RemarkBuilder append(String name, T from, T to) {
        if (Objects.equals(from, to)) {
            return this;
        }

        final String fromStr = parseStr(from);
        final String toStr = parseStr(to);

        sb.append(name)
                .append(" 由 ")
                .append(fromStr)
                .append(" 改为 ")
                .append(toStr)
                .append(";");

        return this;
    }

    private <T> String parseStr(T t) {
        if (t instanceof HashID) {
            return Objects.toString(((HashID) t).getDesc(), "空");
        }
        return Objects.toString(t, "空");
    }

    public String build() {
        return sb.toString();
    }

}
